package dao

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/system_admin"
)

// GetPageOffset 获取分页Offset
func GetPageOffset(page, limit int) int {
	offset := -1
	if page > 0 {
		offset = (page - 1) * limit
	}
	return offset
}

// GetPageLimit 获取分页的offset
func GetPageLimit(limit int) int {
	if limit <= 0 {
		limit = -1
	}
	return limit
}

func GetOffsetLimit(page, size int) (offset int, limit int) {
	if page <= 0 {
		page = 0
	}
	if size <= 0 {
		size = 0
	}
	if page <= 0 && size <= 0 {
		return -1, -1
	}
	if size >= 2000 {
		limit = 2000
	} else {
		limit = size
	}
	offset = (page - 1) * limit
	return
}

// GetFamilyMerchants 获取直系商户数组
func GetFamilyMerchants(merchantID string) (merchantIDs []string) {
	cli, err := client.GetSystemAdminClient()
	if err != nil {
		return nil
	}
	rsp, err := cli.GetFamilyMerchants(context.Background(), &system_admin.FamilyMerchantsReq{
		MerchantId: merchantID,
	})
	if err == nil && rsp != nil {
		return rsp.MerchantIds
	}
	return nil
}
